home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2.sit / Raven 1.2 / Source / Foundation / Common / ZBestFitAllocator.cpp < prev    next >
Text File  |  1997-07-30  |  5KB  |  207 lines

  1. /*
  2.  *  File:       ZBestFitAllocator.cpp
  3.  *  Summary:    An allocator that uses the ODMemMgr from MacApp.
  4.  *  Written by: Jesse Jones
  5.  *
  6.  *  Copyright ゥ 1997 Jesse Jones. 
  7.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  8.  *
  9.  *  Change History (most recent first):    
  10.  *
  11.  *         <2>     7/30/97    JDJ        TBestFitAllocator::ValidateHeap ASSERTs that hook
  12.  *                                    is not nil.
  13.  *         <1>     1/30/97    JDJ        Created
  14.  */
  15.  
  16. #include <ZBestFitAllocator.h>
  17.  
  18. #include <BestFitH.h>
  19. #include <ZUnitTest.h>
  20.  
  21.  
  22. //-----------------------------------
  23. //    Forward References
  24. //
  25. class BestFitHeap;
  26.  
  27.  
  28. //-----------------------------------
  29. //    Static Globals
  30. //
  31. static BlockValidateHook sHook = nil;
  32.  
  33.  
  34. //---------------------------------------------------------------
  35. //
  36. // ValidateHeap
  37. //
  38. //---------------------------------------------------------------
  39. #if DEBUG
  40. static Boolean ValidateHeap( const void* block, unsigned long size, Boolean isObject, void *refCon)
  41. {
  42.     #pragma unused(isObject)
  43.     
  44.     ASSERT(sHook != nil);
  45.     
  46.     (*sHook)(block, (long) (size - PrivBestFitBlock::kBusyOverhead), refCon);
  47.     
  48.     return true;
  49. }
  50. #endif
  51.  
  52. #pragma mark -
  53.  
  54. // ===================================================================================
  55. //    class TBestFitAllocator
  56. // ===================================================================================
  57.  
  58. //---------------------------------------------------------------
  59. //
  60. // TBestFitAllocator::~TBestFitAllocator
  61. //
  62. //---------------------------------------------------------------
  63. TBestFitAllocator::~TBestFitAllocator()
  64. {
  65.     delete mHeap;
  66. }
  67.  
  68.  
  69. //---------------------------------------------------------------
  70. //
  71. // TBestFitAllocator::TBestFitAllocator
  72. //
  73. //---------------------------------------------------------------
  74. TBestFitAllocator::TBestFitAllocator(ulong initialSize, ulong poolSize, ulong hugeSize, MMHeapLocation heap)
  75. {
  76.     mHeap = new (kMMAppMemory) BestFitHeap(initialSize, poolSize, hugeSize, heap);
  77.     mHeap->IBestFitHeap();
  78. }
  79.  
  80.  
  81. //---------------------------------------------------------------
  82. //
  83. // TBestFitAllocator::Allocate
  84. //
  85. //---------------------------------------------------------------
  86. void* TBestFitAllocator::Allocate(ulong bytes)
  87. {
  88.     return mHeap->Allocate(bytes);
  89. }
  90.  
  91.  
  92. //---------------------------------------------------------------
  93. //
  94. // TBestFitAllocator::Deallocate
  95. //
  96. //---------------------------------------------------------------
  97. void TBestFitAllocator::Deallocate(void* block)
  98. {
  99.     if (block != nil)
  100.         mHeap->Free(block);
  101. }
  102.  
  103.  
  104. //---------------------------------------------------------------
  105. //
  106. // TBestFitAllocator::GetHeapSize
  107. //
  108. //---------------------------------------------------------------
  109. ulong TBestFitAllocator::GetHeapSize() const
  110. {
  111.     return mHeap->HeapSize();
  112. }
  113.  
  114.  
  115. //---------------------------------------------------------------
  116. //
  117. // TBestFitAllocator::GetPoolCount
  118. //
  119. //---------------------------------------------------------------
  120. ulong TBestFitAllocator::GetPoolCount() const
  121. {
  122.     return mHeap->GetSegmentCount() - 1UL;
  123. }
  124.  
  125.  
  126. //---------------------------------------------------------------
  127. //
  128. // TBestFitAllocator::GetBlockSize
  129. //
  130. //---------------------------------------------------------------
  131. ulong TBestFitAllocator::GetBlockSize(const void* ptr) const
  132. {
  133.     return mHeap->BlockSize(ptr);
  134. }
  135.  
  136.  
  137. //---------------------------------------------------------------
  138. //
  139. // TBestFitAllocator::GetTotalBlockSize
  140. //
  141. //---------------------------------------------------------------
  142. ulong TBestFitAllocator::GetTotalBlockSize(const void* ptr) const
  143. {
  144.     return mHeap->BlockSize(ptr) + PrivBestFitBlock::kBusyOverhead;
  145. }
  146.  
  147.  
  148. //---------------------------------------------------------------
  149. //
  150. // TBestFitAllocator::ValidateBlock
  151. //
  152. //---------------------------------------------------------------
  153. #if DEBUG
  154. void TBestFitAllocator::ValidateBlock(const void* ptr) const
  155. {
  156.     ASSERT(ptr != nil);
  157.     ASSERT(mHeap->IsValidBlock(ptr));
  158. }
  159. #endif
  160.  
  161.  
  162. //---------------------------------------------------------------
  163. //
  164. // TBestFitAllocator::ValidateHeap
  165. //
  166. //---------------------------------------------------------------
  167. #if DEBUG
  168. void TBestFitAllocator::ValidateHeap(BlockValidateHook hook, void* refCon) const
  169. {
  170.     ASSERT(sHook == nil);
  171.     ASSERT(hook != nil);
  172.     
  173.     sHook = hook;
  174.     
  175.     mHeap->Check(::ValidateHeap, refCon);
  176.     
  177.     sHook = nil;
  178. }
  179. #endif
  180.  
  181. #pragma mark -
  182.  
  183. // ===================================================================================
  184. //    Unit Test
  185. // ===================================================================================
  186.  
  187. //---------------------------------------------------------------
  188. //
  189. // TesTBestFitAllocator
  190. //
  191. // See TAllocator::TestAllocator for allocator comparisons.
  192. //
  193. //---------------------------------------------------------------
  194. #if DEBUG
  195. static void TesTBestFitAllocator()
  196. {
  197.     TBestFitAllocator heap1(64*1024L, 32*1024L, 16*1024);
  198.     TBestFitAllocator heap2(64*1024L, 32*1024L, 16*1024);
  199.     TBestFitAllocator heap3(64*1024L, 32*1024L, 16*1024);
  200.  
  201.     TAllocator::TestAllocator(heap1, heap2, heap3);
  202. }
  203.  
  204. static TUnitTestRegistrar sBestFitAllocatorReg("BestFit Allocator", TesTBestFitAllocator);
  205.  
  206. #endif    // DEBUG
  207.